//
//  PPAlbumObject.m
//  pickpic
//
//  Created by Jinxiao on 10/3/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumObject.h"

@implementation PPAlbumObject

+ (instancetype)albumForParameters:(NSDictionary *)parameters {

    PPAlbumObject *album = [[PPAlbumObject alloc] init];

    NSString *avatar = [parameters validStringForKey:@"ownerAvatar"];
    NSString *avatarHost = [parameters validStringForKey:@"ownerAvatarHost"];
    if([avatar hasPrefix:@"http:"]) {
        album.ownerAvatar = avatar;
    } else if(avatarHost.length > 0 && avatar.length > 0) {
        album.ownerAvatar = [NSString stringWithFormat:@"%@%@", avatarHost, avatar];
    }

    album.ownerName = [parameters validStringForKey:@"ownerName"];
    album.ownerAccount = [parameters validStringForKey:@"pickpic"];

    album.userID = [parameters validStringForKey:@"user"];
    album.albumID = [parameters validNumberForKey:@"albumid"];
    album.coverHost = [parameters validStringForKey:@"coverImgHost"];
    album.coverKey = [parameters validStringForKey:@"coverImg"];
    album.desc = [parameters validStringForKey:@"description"];
    album.title = [parameters validStringForKey:@"title"];

    album.order = [parameters validNumberForKey:@"order"];
    album.permission = [parameters validNumberForKey:@"comment"];
    album.privacy = [parameters validNumberForKey:@"privacy"];

    album.createDate = [NSDate dateWithTimeIntervalSince1970:[parameters validNumberForKey:@"createTime"].doubleValue/1000.0];
    album.updateDate = [NSDate dateWithTimeIntervalSince1970:[parameters validNumberForKey:@"modifyTime"].doubleValue/1000.0];

    album.audit = [parameters validNumberForKey:@"audit"].integerValue;

    album.isLiked = [parameters validNumberForKey:@"like"].boolValue;
    album.isFavorited = [parameters validNumberForKey:@"favorite"].boolValue;

    album.like = [parameters validNumberForKey:@"likedCount"].integerValue;
    album.favorite = [parameters validNumberForKey:@"favoriteCount"].integerValue;

    NSMutableArray <PPTimelineCommentObject *> *comments = [NSMutableArray array];
    for(NSDictionary *parameter in [parameters validArrayForKey:@"commentItems"]) {
        PPTimelineCommentObject *comment = [PPTimelineCommentObject commentForParameters:parameter];
        [comments addSafeObject:comment];
    }
    album.comments = comments;

    album.commentCount = [parameters validNumberForKey:@"commentsCount"].integerValue;

    NSMutableArray <PPTimelinePictureObject *> *pictures = [NSMutableArray array];
    for(NSDictionary *parameter in [parameters validArrayForKey:@"pictures"]) {
        PPTimelinePictureObject *picture = [PPTimelinePictureObject pictureForParameters:parameter];
        [pictures addSafeObject:picture];
    }
    album.pictures = pictures;

    return album;
}

- (NSString *)coverURLString {
    if([self.coverKey length] == 0 || [self.coverHost length] == 0) {
        return nil;
    }

    return [NSString stringWithFormat:@"%@%@", self.coverHost, self.coverKey];
}

- (NSString *)existingCoverURLString {
    NSString *coverURLString = self.coverURLString;

    if(coverURLString == nil) {
        coverURLString = self.pictures.firstObject.URLString;
    }

    return coverURLString;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.userID forKey:@"userID"];
    [encoder encodeObject:self.albumID forKey:@"albumID"];
    [encoder encodeObject:self.ownerAvatar forKey:@"ownerAvatar"];
    [encoder encodeObject:self.ownerName forKey:@"ownerName"];
    [encoder encodeObject:self.ownerAccount forKey:@"ownerAccount"];
    [encoder encodeObject:self.permission forKey:@"permission"];
    [encoder encodeObject:self.coverKey forKey:@"coverKey"];
    [encoder encodeObject:self.coverHost forKey:@"coverHost"];
    [encoder encodeObject:self.title forKey:@"title"];
    [encoder encodeObject:self.desc forKey:@"desc"];
    [encoder encodeObject:self.privacy forKey:@"privacy"];
    [encoder encodeObject:self.order forKey:@"order"];
    [encoder encodeObject:self.createDate forKey:@"createDate"];
    [encoder encodeInteger:self.audit forKey:@"audit"];
    [encoder encodeInteger:self.like forKey:@"like"];
    [encoder encodeInteger:self.favorite forKey:@"favorite"];
    [encoder encodeBool:self.isLiked forKey:@"isLiked"];
    [encoder encodeBool:self.isFavorited forKey:@"isFavorited"];
    [encoder encodeInteger:self.commentCount forKey:@"commentCount"];
    [encoder encodeObject:self.comments forKey:@"comments"];
    [encoder encodeObject:self.pictures forKey:@"pictures"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.userID = [decoder decodeObjectForKey:@"userID"];
        self.albumID = [decoder decodeObjectForKey:@"albumID"];
        self.ownerAvatar = [decoder decodeObjectForKey:@"ownerAvatar"];
        self.ownerName = [decoder decodeObjectForKey:@"ownerName"];
        self.ownerAccount = [decoder decodeObjectForKey:@"ownerAccount"];
        self.permission = [decoder decodeObjectForKey:@"permission"];
        self.coverKey = [decoder decodeObjectForKey:@"coverKey"];
        self.coverHost = [decoder decodeObjectForKey:@"coverHost"];
        self.title = [decoder decodeObjectForKey:@"title"];
        self.desc = [decoder decodeObjectForKey:@"desc"];
        self.privacy = [decoder decodeObjectForKey:@"privacy"];
        self.order = [decoder decodeObjectForKey:@"order"];
        self.createDate = [decoder decodeObjectForKey:@"createDate"];
        self.audit = [decoder decodeIntegerForKey:@"audit"];
        self.like = [decoder decodeIntegerForKey:@"like"];
        self.favorite = [decoder decodeIntegerForKey:@"favorite"];
        self.isLiked = [decoder decodeBoolForKey:@"isLiked"];
        self.isFavorited = [decoder decodeBoolForKey:@"isFavorited"];
        self.commentCount = [decoder decodeIntegerForKey:@"commentCount"];
        self.comments = [decoder decodeObjectForKey:@"comments"];
        self.pictures = [decoder decodeObjectForKey:@"pictures"];
    }
    return self;
}

- (void)queryCoverImageWithCompletion:(void (^)(UIImage *, NSError *))completion {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSString *URLString = self.existingCoverURLString;

        if(URLString.length > 0) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:URLString] options:kNilOptions progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                !completion ?: completion(image, error);
            }];
        } else {
            !completion ?: completion(nil, [NSError errorWithDomain:@"com.pickpic.error" code:0 userInfo:@{NSLocalizedDescriptionKey: @"相册封面加载失败"}]);
        }
    });
}

@end
